package de.tum.in.eist.poll.client;

import java.util.Date;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;

import de.tum.in.eist.poll.shared.Feedback;
import de.tum.in.eist.poll.shared.Lecture;

/**
 * Page to show the statistics to the teacher.
 * 
 * @author florian
 * 
 */
public class Statistics extends Webpage {

	private static StatisticsUiBinder uiBinder = GWT
			.create(StatisticsUiBinder.class);
	@UiField
	ListBox lecture;
	@UiField
	ListBox date;
	@UiField
	HTMLPanel statistic;
	@UiField
	Button refreshStat;
	@UiField
	HTMLPanel notification;

	Statistik chart;

	interface StatisticsUiBinder extends UiBinder<Widget, Statistics> {
	}

	/**
	 * Index of the selected Lecture.
	 */
	private int selectedLectureIndex;

	@Override
	public final void loadWebsite() {
		setContent(uiBinder.createAndBindUi(this));

		lecture.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(final ClickEvent event) {
				if (selectedLectureIndex != lecture.getSelectedIndex()) {
					selectedLectureIndex = lecture.getSelectedIndex();
					fillDates();
					showStatistics();
				}
			}
		});

		getService().requestLectures(new AsyncCallback<List<Lecture>>() {

			@Override
			public void onFailure(final Throwable caught) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onSuccess(final List<Lecture> result) {
				for (Lecture l : result) {
					lecture.addItem(l.getLectureTitle(), l.getLectureID());
				}
				lecture.setSelectedIndex(0);
				selectedLectureIndex = 0;
				fillDates();
				showStatistics();
			}
		});

		refreshStat.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				showStatistics();
				statistic.setVisible(false);
				getService().registerForNewPollData(
						lecture.getValue(lecture.getSelectedIndex()),
						pushCallback);
			}
		});
	}

	/**
	 * fill the Date-ListBox with date entries received from the server.
	 */
	private void fillDates() {
		date.setEnabled(false);
		date.clear();
		date.addItem("All", "");

		getService().getLectureDates(
				lecture.getValue(lecture.getSelectedIndex()),
				new AsyncCallback<List<Date>>() {

					@Override
					public void onFailure(final Throwable caught) {
						// TODO Auto-generated method stub
					}

					@Override
					public void onSuccess(final List<Date> result) {
						for (Date d : result) {
							date.addItem(d.toString(), d.toString());
						}
						date.setEnabled(true);
					}
				});
	}

	/**
	 * Show the statistics of the feedback gotten from the students.
	 */
	private void showStatistics() {
		String lectureID = lecture.getValue(lecture.getSelectedIndex());
		getService().requestFeedback(lectureID, null,
				new AsyncCallback<List<Feedback>>() {

					@Override
					public void onFailure(final Throwable caught) {
						// TODO Auto-generated method stub
					}

					@Override
					public void onSuccess(final List<Feedback> result) {
						statistic.add(new Label("Students liked:"));
						for (Feedback feedback:result){
							statistic.add(new Label(feedback.getQuestion2()));
						}
						
						statistic.add(new Label("Students disliked:"));
						for (Feedback feedback:result){
							statistic.add(new Label(feedback.getQuestion2()));
						}
						
						if (chart != null) {
							statistic.remove(chart);
						}
						chart = new Statistik(result);
						statistic.add(chart);
						chart.update();
					}
				});

		getService().registerForNewPollData(lectureID, pushCallback);
	}

	AsyncCallback<String> pushCallback = new AsyncCallback<String>() {

		@Override
		public void onSuccess(final String result) {
			if (result.equals(lecture.getValue(lecture.getSelectedIndex()))) {
				notification.setVisible(true);
			}
		}

		@Override
		public void onFailure(final Throwable caught) {
			// TODO Auto-generated method stub

		}
	};
}
